<?php
	include_once("connect.php");
	include("post_model.php");
	mysql_select_db("progin_171_13509047",$con);
	 if(isset($_POST['username'])) $post_user =$_POST['username'];
	if(isset($_POST['password']))
		$post_pass = $_POST['password'];
	if(isset($_POST['confirm_password']))
		$post_confirm_pass = $_POST['confirm_password'];
	if(isset($_POST['email']))
		$post_email = $_POST['email'];
	if(isset($_POST['real_name']))
		$post_full_name = $_POST['real_name'];
	if(isset($_POST['confirm_password']))
		$post_conf_pass=$_POST['confirm_password'];
	if(isset($_POST['birthday']))
		$post_birthday = $_POST['birthday'];
	if(isset($_POST['sex']))
		$post_gender = $_POST['sex'];
	if(isset($_POST['about']))
		$post_about = $_POST['about'];
	
	$p_model = new Post_Model();
	$err_msg="";	//pesan error
	$change_name = explode(".",$_FILES['avatar']['name']);
	$new_filename = $post_user.".".$change_name[1]; //ganti nama file sesuai dengan username

	$sql_insert = "INSERT INTO user (username, password, email, gender, full_name, birth, avatar, about_me) VALUES ('$post_user',MD5('$post_pass'),'$post_email','$post_gender','$post_full_name','$post_birthday','$new_filename', '$post_about')";
	$sql_validation_user = "SELECT username,email FROM user WHERE username='$post_user'";
	$sql_validation_email = "SELECT username,email FROM user WHERE email='$post_email'";
	$dbvalidation_user=mysql_query($sql_validation_user);
	$dbvalidation_email=mysql_query($sql_validation_email);
	if(validateAll())
	{
		mysql_query($sql_insert);
		uploadfile($new_filename);
		$uid = mysql_fetch_array($p_model->getUserIdByUsername($post_user));
		header('Location: profile.php?id='.$uid['ID']);
	}
	else
	{
		$setparam = (!empty($post_user))? "&username=$post_user":"";
		$setparam = (!empty($post_email))? $setparam."&email=$post_email":$setparam."";
		$setparam = (!empty($post_full_name))? $setparam. "&real_name=$post_full_name":$setparam. "";
		$setparam = (!empty($post_birthday))? $setparam."&birthday=$post_birthday":$setparam."";
		$setparam = (!empty($post_gender))? $setparam. "&sex=$post_gender":$setparam. "";
		$setparam = (!empty($post_about))? $setparam."&about=$post_about":$setparam."";
		header('Location: register.php?err_msg='.$err_msg."$setparam"); 
	} 

//validasi form
// 1: username < 5 karakter
// 2: password < 8 karakter
// 3: username == pasword
// 4: password == email
// 5: konfirmasi password salah
// 6: nama tidak valid (minimal mengandung spasi diantara 2 karakter)
// 7: birthday tidak valid (YYYY-MM-DD)
// 8: email tidak valid
// 9: avatar tidak valid (.jpeg atau .jpg saja)
//10: gender belum dipilih
//11: file gagal diupload
	function validateAll()
	{
		global $err_msg;//inisialisasi
		global $post_user,$post_pass,$post_confirm_pass,$post_email,$post_full_name,$post_conf_pass,$post_birthday,$post_gender;
		global $dbvalidation_user,$dbvalidation_email,$new_filename;
		//validasi form
		if(strlen($post_user)<5)//panjang username <5
		{
/* 			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			} */
			$err_msg=$err_msg.'1';
		}
		
		if(strlen($post_pass)<8) //panjang password<8
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'2';
		}
		if(strcmp($post_user,$post_pass)==0)//username == password
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'3';
		}
		if(strcmp($post_pass,$post_email)==0)
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'4';
		}
		if(strcmp($post_pass,$post_confirm_pass) != 0)// konfirmasi password salah
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'5';
		}
		if(!cekNama($post_full_name))
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'6';
		
		}
		if(!cekBirthday($post_birthday))
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'7';
		
		}
		if(!cekEmail($post_email))//cek email
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'8';
		}
		if(!cekAvatarType())
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'9';
		}
		else
		{
			if(!isUploadSucceed())
			{
					
				if(isset($err_msg))
				{
					$err_msg=$err_msg.'l';
				}
				$err_msg=$err_msg."11";//gagal mengupload file
			}
		}
		if(strcmp($post_gender,"select_gender")==0)
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'10';
		
		}
		if (mysql_num_rows($dbvalidation_user)>0)
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'12';
		}
		if (mysql_num_rows($dbvalidation_email)>0)
		{
			if(isset($err_msg))
			{
				$err_msg=$err_msg.'|';
			}
			$err_msg=$err_msg.'13';
		}
		return empty($err_msg);
	}
	
function cekEmail($string)
{
	$pattern="/\S+@\S+\.\S+\S+/";
	return(preg_match($pattern,$string)==1);
	
}
function cekNama($string)
{
	$pattern="/\S+ \S+/";
	return(preg_match($pattern,$string)==1);
}
function cekBirthday($string)
{
	$pattern="/\d{4}-\d{2}-\d{2}/";
	return(preg_match($pattern,$string)==1);
}
function cekAvatarType()
{
	if (($_FILES["avatar"]["type"] == "image/jpeg")|| ($_FILES["avatar"]["type"] == "image/pjpeg")&& ($_FILES["avatar"]["size"] < 20000))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function isUploadSucceed()
{

  if ($_FILES["avatar"]["error"] > 0)
    {
		return false;
    }
  else
    {
		return true;
	}
}

function uploadfile($filename)
{
	move_uploaded_file($_FILES['avatar']['tmp_name'],"avatar/".$filename);
}

?>